﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcTricks.RoundTripModelBinding;
using MvcTricks.RoundTripModelBinding.TestWebsite.ViewModels;

namespace MvcTricks.RoundTripModelBinding.TestWebsite.Controllers
{
    public class ShoppingController : Controller
    {

        private ProductCataloqueViewModel CreateDummuProductCataloque()
        {
            var cataloque = new ProductCataloqueViewModel();
            cataloque.ProductCategories = new ProductCategoryViewModel[] {
                new ProductCategoryViewModel() {
                    CategoryId = 1,
                    CategoryName = "Running shoes",
                    Products = new ProductViewModel[] {
                        new ProductViewModel() {
                            ProductId = 1,
                            SKU = "MIZS01",
                            ProductName = "Mizuno Wave Rider 14",
                            Price = 71.99
                        },
                        new ProductViewModel() {
                            ProductId = 2,
                            SKU = "ASIS01",
                            ProductName = "Asics Gel-Nimbus 12",
                            Price = 86.49
                        },
                    }
                },
                new ProductCategoryViewModel() {
                    CategoryId = 2,
                    CategoryName = "Running Jackets",
                    Products = new ProductViewModel[] {
                        new ProductViewModel() {
                            ProductId = 3,
                            SKU = "NIKJ01",
                            ProductName = "Nike Essential Soft Hand 1/2 Zip",
                            Price = 19.99
                        },
                        new ProductViewModel() {
                            ProductId = 4,
                            SKU = "ADIJ01",
                            ProductName = "Adidas Supernova Convertible Windjacket",
                            Price = 24.99
                        },
                    }
                },
                new ProductCategoryViewModel() {
                    CategoryId = 3,
                    CategoryName = "Running Tights",
                    Products = new ProductViewModel[] {
                        new ProductViewModel() {
                            ProductId = 5,
                            SKU = "RONT01",
                            ProductName = "Ronhill Pursuit Tight",
                            Price = 22.99
                        },
                        new ProductViewModel() {
                            ProductId = 6,
                            SKU = "NIKT01",
                            ProductName = "Nike Tech Tight",
                            Price= 19.99
                        },
                    }
                }
            };
            return cataloque;
        }

        public ActionResult Index()
        {
            var model = CreateDummuProductCataloque();
            ViewData["Basket"] = new ShoppingBasketViewModel() { OrderLines = new List<OrderLineViewModel>() };
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(ProductCataloqueViewModel model)
        {
            var basket = this.GetRoundTripModel<ShoppingBasketViewModel>("Basket");
            if (basket.OrderLines.Count() == 0)
            {
                basket.OrderLines.Add(new OrderLineViewModel()
                {
                    Product = model.ProductCategories.First().Products.First(),
                    Quantity = 1
                });
            }
            else
                basket.OrderLines.First().Quantity++;
            ViewData["Basket"] = basket;
            return View(model);
        }

    }
}
